//
//  XZLotteryTypeView.m
//  生活小助手
//
//  Created by MAC－ME on 14-8-7.
//  Copyright (c) 2014年 lxz. All rights reserved.
//

#define btnH 20
/**通知**/
#define LottoryTypeViewOnClickNotification @"LottoryTypeViewOnClickNotification"

#import "XZLotteryTypeView.h"
#import "XZLotteryType.h"

@interface XZLotteryTypeView ()
@property (strong,nonatomic) UIScrollView  *scrollView;
@end

@implementation XZLotteryTypeView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor darkGrayColor];
        
    }
    return self;
}
-(void)setTypeArry:(NSArray *)typeArry
{
    _typeArry = typeArry;
    
    self.height = self.typeArry.count * btnH;
    
    self.contentSize = CGSizeMake(self.width, self.height);
    
    [self setupTypeButton];

    [self setNeedsLayout];
}
- (void)setupScrollView
{
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    [self addSubview:scrollView];
    scrollView.backgroundColor = [UIColor redColor];
    self.scrollView = scrollView;
    NSLog(@"subviews =%@",self.subviews);
}
- (void)setupTypeButton
{
   
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    for (XZLotteryType  *type in self.typeArry) {
        UIButton *btn = [[UIButton alloc]init];
        [btn setTitle:type.lottery forState:UIControlStateNormal];
        btn.tag = type.lotteryId;
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        [btn addTarget:self action:@selector(typeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
    }
}

- (void)typeBtnClick:(UIButton *)btn
{
    [[NSNotificationCenter defaultCenter]postNotificationName:LottoryTypeViewOnClickNotification object:nil userInfo:@{@"loyType":btn}];
    [self dismiss];
}

- (void)dismiss
{
    self.hidden = YES;
}
- (void)layoutSubviews
{
    [super layoutSubviews];

    int count = self.subviews.count;
    
    if (count > 8) {
        self.height = 8 * btnH;
    }
    else
    {
        self.height = count * btnH;
    }
    
    for (int i = 0; i <count ; i++) {
        UIButton *btn = self.subviews[i];
        btn.width = self.frame.size.width;
        btn.x = 0;
        btn.y = i * btnH;
        btn.height = btnH;
    }
}

@end
